package com.woniuxy.oldmanapi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.cvo.OverdueDetailVo;
import com.woniuxy.oldmanapi.cvo.OverdueVo;
import com.woniuxy.oldmanapi.entity.Overdue;
import com.woniuxy.oldmanapi.mapper.MedicineMapper;
import com.woniuxy.oldmanapi.mapper.OverdueDetailMapper;
import com.woniuxy.oldmanapi.mapper.OverdueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class OverdueService {
    @Autowired
    OverdueMapper overdueMapper;
    @Autowired
    OverdueDetailMapper overdueDetailMapper;
    @Autowired
    MedicineMapper medicineMapper;
    public PageInfo<OverdueVo> findOverdueVosByCons(String outname, int state, int size, int now) {
        PageHelper.startPage(now,size);
        List<OverdueVo> list=overdueMapper.selectOverduevoByCons(outname,state);
        return new PageInfo<>(list);
    }

    //添加-订单，明细
    @Transactional
    public void addOverdue(OverdueVo overdueVo) {
        Overdue overdue=new Overdue();
        overdue.setCreatetime(overdueVo.getCreatetime());
        overdue.setDesc(overdueVo.getDesc());
        String s = validateCode();
        overdue.setOrdernum(s);
        overdue.setState(0);
        overdue.setTotalprice(overdueVo.getTotalprice());
        overdue.setOutname(overdueVo.getOutname());
        overdue.setOuttime("");
        overdueMapper.insertOverdue(overdue);

        List<OverdueDetailVo> overdueDetailVos = overdueVo.getOverdueDetailVos();
        Date createtime = overdueVo.getCreatetime();
        Integer id = overdue.getId();
        overdueDetailMapper.insertBeach(createtime,id,overdueDetailVos);

    }

    //生成订单编号
    private String validateCode(){
        String data = "0123456789" ;
        String str = "WNoverdue";
        Random random = new Random();
        for (int i=0;i<5;i++){
            //获取 0 --9 随机数
            int k = random.nextInt(10);
            str += data.charAt(k);
        }
        return str ;
    }

    //编辑==订单修改，删除明细，添加明细
    @Transactional
    public void modifyOverdue(OverdueVo overdueVo) {
        Overdue overdue=new Overdue();
        overdue.setCreatetime(overdueVo.getCreatetime());
        overdue.setDesc(overdueVo.getDesc());
        overdue.setOrdernum(overdueVo.getOrdernum());
        overdue.setState(overdueVo.getState());
        overdue.setTotalprice(overdueVo.getTotalprice());
        overdue.setOutname(overdueVo.getOutname());
        overdue.setId(overdueVo.getId());
        overdue.setOuttime("");
        overdueMapper.updateOverdue(overdue);

        List<OverdueDetailVo> overdueDetailVos = overdueVo.getOverdueDetailVos();
        Date createtime = overdueVo.getCreatetime();
        Integer id = overdue.getId();
        overdueDetailMapper.deleteByOverdueId(id);
        overdueDetailMapper.insertBeach(createtime,id,overdueDetailVos);
    }

    //撤销
    public void cancelOverdue(int id) {
      int  state=2;
      overdueMapper.updateState(id,state);
    }

    //出库==库存，状态
    @Transactional
    public void putOverdue(OverdueVo overdueVo) {
        Integer overdueId = overdueVo.getId();
        int state=1;
        overdueMapper.updateState(overdueId,state);

        List<OverdueDetailVo> overdueDetailVos = overdueVo.getOverdueDetailVos();
        for(OverdueDetailVo o:overdueDetailVos){
            Integer medicineId = o.getMedicine().getId();
            Integer count = o.getCount();
            medicineMapper.loseScoreCount(medicineId,count);
        }
    }
}
